<?php

/**
 * 自动生成 SysUserService
 * 
 * Apps/Common/Service/SysUserService.class.php
 *  @ 2015-06-24
 */

namespace Common\Service;

class SysUserService {

  private static $instance;

  public static function instance() {
    if (self::$instance == null) {
      $instance = new SysUserService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'group_id' => '',
      'username' => '',
      'password' => '',
      'email' => '',
      'phone' => '',
      'status' => 7 ,
      'verify_email' => 0,
      'verify_phone' => 0,
      'token' => '',
      'token_expired' => '',
      'create_time' => date('Y-m-d H:i:s'),
      'update_time' => date('Y-m-d H:i:s'),
      'last_login' => date('Y-m-d H:i:s'),
    );
  }

  function get_by_id($id) {
    $SysUser = M('SysUser');
    $data = $SysUser->find($id);
    return $data ? $data : array();
  }
  
  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'sys_type' => '' ,
      'keyword' => '',
      'time_from' => '',
      'time_to' => '',
      'role_id' => '',
      'role_rank' => 0 ,
      'status' => '',
      'mer_id' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $SysUser = M('SysUser');

    $where = array();

    if (!empty($config['keyword'])) {
      $k = $config['keyword'];
      $where['_string'] = " su.username like '%$k%' or su.phone = '$k' or su.email = '$k'";
    }
    
    if (!empty($config['status'])) {
      $where['su.status'] = $config['status'];
    }
    
    if (!empty($config['sys_type'])) {
      $where['su.sys_type'] = $config['sys_type'];
    }
    
    if (!empty($config['time_from'])) {
      $where['su.last_login'][] = array('EGT' , $config['time_from'] );
    }
    
    if (!empty($config['time_to'])) {
      $where['su.last_login'][] = array('ELT' , $config['time_to'] );
    }
    
    if (!empty($config['role_id'])) {
      $where['sur.role_id'] = $config['role_id'];
    }
    
    if ( !empty( $config['role_rank'] ) ) {
      $where['sr.rank'] = array('LT' , $config['role_rank']);
    }
       
    if ($config['count']) {
      return $SysUser
          ->alias('su')
          ->join('sys_user_role sur ON sur.user_id = su.id')
          ->join('sys_role sr ON sr.id = sur.role_id' , 'left')
          ->join('sys_dictionary sd ON sd.id = su.sys_type' , 'left')
          ->where($where)
          ->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $sql = '(SELECT GROUP_CONCAT(role_id) FROM sys_user_role WHERE user_id = su.id ) AS role_id ,'
        . '(SELECT GROUP_CONCAT(sr.name) FROM sys_role sr WHERE id IN ( SELECT role_id FROM sys_user_role WHERE user_id = su.id )) AS role_name ,'
        . '(SELECT GROUP_CONCAT(group_id) FROM sys_user_group WHERE user_id = su.id ) AS group_id ,'
        . '(SELECT GROUP_CONCAT(sg.name) FROM sys_group sg WHERE id IN ( SELECT group_id FROM sys_user_group WHERE user_id = su.id )) AS group_name ';
      $data = $SysUser
          ->alias('su')
          ->field('DISTINCT su.id AS d_id , su.* , sd.text AS sts_type_name ,' . $sql )
          ->where($where)
          ->join('sys_user_role sur ON sur.user_id = su.id')
          ->join('sys_role sr ON sr.id = sur.role_id' , 'left')
          ->join('sys_dictionary sd ON sd.id = su.sys_type' , 'left')
          ->limit($limit)
          ->order($order)
          ->select();
      //echo $SysUser->_sql();
      return $data ? $data : array();
    }
  }

  function create(  $data ) {
    //先添加用户
    $SysUser = M('SysUser');
    
    $ret = $SysUser->add( $data );

    if ( $ret ) {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    } else {
      return ajax_arr('添加失败') ;
    }
  }

  function update($id, $data) {
    $SysUser = M('SysUser');

    $old_data = $SysUser->where("id <> $id and username='". $data['username'] ."' ")->find();
    if ( !empty( $old_data ) ) {
      return ajax_arr('已经有同名用户了') ;
    }
    $ret = $SysUser->where('id = %d' , $id )->save( $data );

    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $SysUser = M('SysUser');
    $ret = $SysUser->delete($ids);

    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
  
  function get_mer_manager_user_by_id( $id ) {
    $MerUserManager = M('MerUserManager');
    $data = $MerUserManager->where('id = %d' , $id )->find();
    return $data ? $data : array();
  }
  
  function get_mer_manager_user( $mer_id ) {
    $MerUserManager = M('MerUserManager');
    
    $sql = '(SELECT GROUP_CONCAT(role_id) FROM sys_user_role WHERE user_id = su.id ) AS role_id ,'
      . '(SELECT GROUP_CONCAT(sr.name) FROM sys_role sr WHERE id IN ( SELECT role_id FROM sys_user_role WHERE user_id = su.id )) AS role_name ,'
      . '(SELECT GROUP_CONCAT(group_id) FROM sys_user_group WHERE user_id = su.id ) AS group_id ,'
      . '(SELECT GROUP_CONCAT(sg.name) FROM sys_group sg WHERE id IN ( SELECT group_id FROM sys_user_group WHERE user_id = su.id )) AS group_name ';

    $data = $MerUserManager
      ->field('mum.* , su.username , su.email , su.phone , su.status , su.token , su.token_expired , su.create_time , su.update_time , su.last_login ,' .$sql)
      ->alias('mum')
      ->where('mum.mer_id = %d' , $mer_id )
      ->join('sys_user su on su.id = mum.sys_user_id')
      ->join('sys_user_role sur ON sur.user_id = su.id')
      ->join('sys_role sr ON sr.id = sur.role_id' , 'left')
      ->join('sys_dictionary sd ON sd.id = su.sys_type' , 'left')
      ->select();
    //echo $MerUserManager->_sql();
    return $data ? $data : array();
  }
  
  function create_mer_manager_user( $user_id , $mer_id ) {
    $MerUserManager = M('MerUserManager');
    $ret = $MerUserManager->add( array(
      'sys_user_id' => $user_id ,
      'mer_id' => $mer_id 
    ));
        
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }
  
  function delete_mer_manager_user( $id ) {
    $MerUserManager = M('MerUserManager');
    $ret = $MerUserManager->where( 'id = %d' , $id )->delete();    
    
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

}
